package config

import (
	log "github.com/sirupsen/logrus"
	"github.com/spf13/viper"
)

type Config struct {
	Engine       Engine       `mapstructure:"engine"`
	Pulsar       Pulsar       `mapstructure:"pulsar"`
	Metrics      Metrics      `mapstructure:"metrics"`
	TaskAdmin    TaskAdmin    `mapstructure:"task_admin"`
	TaskMonitor  TaskMonitor  `mapstructure:"task_monitor"`
	TaskDispatch TaskDispatch `mapstructure:"task_dispatch"`
	TaskExecutor TaskExecutor `mapstructure:"task_executor"`
	Business     Business     `mapstructure:"business"`
}

type Engine struct {
	Host   string `mapstructure:"host"`
	Port   int64  `mapstructure:"port"`
	User   string `mapstructure:"user"`
	Passwd string `mapstructure:"passwd"`
	DB     string `mapstructure:"db"`
}

type Pulsar struct {
	Address          string `mapstructure:"address"`
	Timeout          int64  `mapstructure:"timeout"`
	DispatchTopic    string `mapstructure:"dispatch_topic"`
	ExecuteTopic     string `mapstructure:"execute_topic"`
	Business1Topic   string `mapstructure:"business1_topic"`
	Business2Topic   string `mapstructure:"business2_topic"`
	CallbackTopic    string `mapstructure:"callback_topic"`
	SubscriptionName string `mapstructure:"subscription_name"`
}

type Metrics struct {
	Address string `mapstructure:"address"`
}

type TaskAdmin struct {
	Name string `mapstructure:"name"`
	Port int64  `mapstructure:"port"`
}

type TaskMonitor struct {
	Name string `mapstructure:"name"`
}

type TaskDispatch struct {
	Name     string `mapstructure:"name"`
	GrpcPort int64  `mapstructure:"grpc_port"`
	RetryCnt int    `mapstructure:"retry_cnt"`
}

type TaskExecutor struct {
	Name string `mapstructure:"name"`
}

type Business struct {
	Name     string `mapstructure:"name"`
	GrpcPort int64  `mapstructure:"grpc_port"`
}

var Conf = new(Config)

func init() {
	var err error
	viper.SetConfigFile("./task.yml") // 指定配置文件路径
	// 读取配置信息
	if err = viper.ReadInConfig(); err != nil {
		log.Panic("Failed to read config:", err)
	}
	// 解析配置信息
	if err = viper.Unmarshal(Conf); err != nil {
		log.Panic("Failed to unmarshal config:", err)
	}
	log.Infof("Read config: %+v", Conf)
}
